<template>
  <div>
    <el-dialog title="修改供应商" :visible.sync="addsuppliervis" @close="close" width="500px" append-to-body>
        <el-form :rules="rules" :model="editSupplierForm" ref="editSupplierForm">

            <el-form-item label="编号:" prop="supplierCode" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.supplierCode" placeholder="请输入供应商编号"></el-input>
            </el-form-item>
            <el-form-item label="名称:" prop="supplierName" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.supplierName" placeholder="请输入供应商名称"></el-input>
            </el-form-item>
            <el-form-item label="地址:" prop="contactAddress" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.contactAddress" placeholder="请输入联系地址"></el-input>
            </el-form-item>
            <el-form-item label="联系人:" prop="contactPerson" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.contactPerson" placeholder="请输入联系人"></el-input>
            </el-form-item>
            <el-form-item label="电话:" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.supplierTel" placeholder="请输入供应商电话"></el-input>
            </el-form-item>
            <el-form-item label="备注:" :label-width="formLabelWidth">
                <el-input v-model="editSupplierForm.supplierRemark" placeholder=""></el-input>
            </el-form-item>
            <el-form-item label="创建时间:" prop="date" :label-width="formLabelWidth">
                <el-date-picker type="date" placeholder="选择日期" v-model="editSupplierForm.supplierCreateDate" style="width: 100%;"></el-date-picker>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="danger" :loading="submitLoading" @click="updateSupplier('editSupplierForm')">确 定</el-button>
        </div>
          </el-dialog>
        </div>
</template>

<script>
import { UpdateSupplier } from '@/api/prod';
export default {
  name: 'addsupplier',
  props: ['openUpdateSupplier','choseSupplier'],
  data() {
    return {
      addsuppliervis: false,
      submitLoading: false,
      editSupplierForm: {
        supplierCode: '',
        supplierName: '',
        supplierTel: '',
        contactAddress: '',
        contactPerson: '',
        supplierRemark: '',
        supplierCreateDate: new Date()
      },
      formLabelWidth: '90px',
      rules: {
          supplierCode: [
            { required: true, message: '请输入供应商编号', trigger: 'blur' },
          ],
          supplierName: [
            { required: true, message: '请输入供应商名称', trigger: 'blur' },
          ],
          contactAddress: [
            { required: true, message: '请输入联系地址', trigger: 'blur' },
          ],
          contactPerson: [
            { required: true, message: '请输入联系人', trigger: 'blur' },
          ],
      }
    }
  },
  methods: {
    close() {
      this.addsuppliervis = false;
      this.resetForm();
      this.$emit("update:openUpdateSupplier", false);
    },
    resetForm() {
      this.editSupplierForm.supplierCode = '';
      this.editSupplierForm.supplierName = '';
      this.editSupplierForm.supplierTel = '';
      this.editSupplierForm.contactAddress = '';
      this.editSupplierForm.contactPerson = '';
      this.editSupplierForm.supplierRemark = '';
    },
    updateSupplier(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
              let reqData = {
                  supplierCode: this.editSupplierForm.supplierCode,
                  supplierName: this.editSupplierForm.supplierName,
                  supplierTel: this.editSupplierForm.supplierTel,
                  contactAddress: this.editSupplierForm.contactAddress,
                  contactPerson: this.editSupplierForm.contactPerson,
                  supplierRemark: this.editSupplierForm.supplierRemark,
                  supplierCreateDate: this.editSupplierForm.supplierCreateDate,
                  supplierId: this.choseSupplier.supplierId
              }
              // this.submitLoading = true;
              UpdateSupplier(reqData).then(response => {
                  let data = response.data
                  this.$message({
                      message: data.msg,
                      type: 'success'
                  })
                  this.submitLoading = false;
                  this.$refs.editSupplierForm.resetFields();
                  this.resetForm();
                  this.addsuppliervis = false;
                  this.$emit("update:openUpdateSupplier", false);
              }).catch(error => {
                  this.submitLoading = false;
              })
            } else {
              console.log('error submit!!');
              return false;
            }
        });
        // this.resetForm()
        
            
        // }
    }
  },
  watch: {
    openUpdateSupplier(newVal,oldVal) {  
      this.addsuppliervis = newVal;
    },
    choseSupplier(newVal) {
      if (newVal) {
        this.editSupplierForm = newVal;
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  
</style>